﻿using Microsoft.AspNetCore.Mvc;
using System;
using WxTenpay.Common;

namespace WXApi.Model
{
    /// <summary>
    /// 返回数据操作类
    /// </summary>
    public class ReqDataControllerBase : ControllerBase
    {


        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <returns></returns>
        public ActionResult Success(object data)
        {
            try
            {
                var redata = new
                {
                    success = true,
                    data = data,

                };
                return Ok(redata);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return Ok(new { success = false, data = "系统异常！" });
            }

        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <returns></returns>
        public ActionResult SuccessLog(SuccessLogDto model)
        {
            try
            {
                model.loginlogmodel.Ip = HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
                if(string.IsNullOrWhiteSpace(model.loginlogmodel.UserGuId))
                model.loginlogmodel.UserGuId = DESEncrypt.MD5Decrypt(HttpContext.Items["token"].ToString());
                model.loginlogmodel.Create();
                model._sysloginlog.Add(model.loginlogmodel);
                var redata = new
                {
                    success = true,
                    data = model.data,

                };
                return Ok(redata);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return Ok(new { success = false, data = "系统异常！" });
            }

        }


        /// <summary>
        /// 自定义返回数据
        /// </summary>
        /// <param name="obj">返回数据</param>
        /// <returns></returns>
        public ActionResult Custom(object obj)
        {
            try
            {
                return Ok(obj);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return Ok(new { success = false, data = "系统异常！" });
            }

        }


        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult Error(object data)
        {
            try
            {
                var redata = new
                {
                    success = false,
                    data = data,

                };
                return Ok(redata);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return Ok(new { success = false, data = "系统异常！" });
            }

        }


        /// <summary>
        /// 失败写错误日志
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult ErrorLog(ErrorLogDto model)
        {
            try
            {

                model.logmodel.Create();
                model._syslog.Add(model.logmodel);
                var redata = new
                {
                    success = false,
                    data = model.data,

                };
                return Ok(redata);
            }
            catch (Exception _ex)
            {
                Console.WriteLine(_ex);
                return Ok(new { success = false, data = "系统异常！" });
            }

        }


        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult Error()
        {
            try
            {
                var redata = new
                {
                    success = false,
                    data = "操作失败！",

                };
                return Ok(redata);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return Ok(new { success = false, data = "系统异常！" });
            }

        }



        /// <summary>
        /// 获取解密用户信息
        /// </summary>
        /// <returns></returns>
        public string GetUser(string token)
        {

            try
            {
                return DESEncrypt.MD5Decrypt(token);
            }
            catch (Exception)
            {

                throw;
            }

        }

    }
}
